#
# Ronin Exploits - A Ruby library for Ronin that provides exploitation and
# payload crafting functionality.
#
# Copyright (c) 2007-2013 Hal Brodigan (postmodern.mod3 at gmail.com)
#
# This file is part of Ronin Exploits.
#
# Ronin is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ronin is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ronin.  If not, see <http://www.gnu.org/licenses/>
#

require 'ronin/ui/cli/resources_command'
require 'ronin/exploits/exploit'

module Ronin
  module UI
    module CLI
      module Commands
        class Exploits < ResourcesCommand

          summary 'Lists available exploits'

          model Ronin::Exploits::Exploit

          query_option :named, type: String,
                               flag: '-n',
                               usage: 'NAME'

          query_option :revision, type: String,
                                  flag: '-V',
                                  usage: 'VERSION'

          query_option :describing, stype: String,
                                    flag: '-d',
                                    usage: 'TEXT'

          query_option :status, type: String,
                                flag: '-s',
                                usage: 'potential|proven|weaponized'

          query_option :licensed_under, type: String,
                                        flag: '-l',
                                        usage: 'LICENSE'

          query_option :targeting_arch, type: String,
                                        flag: '-a',
                                        usage: 'x86|x86_64|ia64|ppc|ppc64|sparc|sparc64|mips|mips_le|arm|arm_le'

          query_option :targeting_os, type: String,
                                      flag: '-o',
                                      usage: 'Linux|FreeBSD|OpenBSD|NetBSD|OSX|Solaris|Windows|UNIX'

          query_option :targeting_software, type: String,
                                            flag: '-S',
                                            usage: 'SOFTWARE'

          protected

          def print_resource(exploit)
            unless verbose?
              puts "  #{exploit}"
              return
            end

            print_section "Exploit: #{exploit}" do
              puts "Name: #{exploit.name}"
              puts "Version: #{exploit.version}"
              puts "Type: #{exploit.type}" if verbose?
              puts "Status: #{exploit.status}"
              puts "Released: #{exploit.released}"
              puts "Reported: #{exploit.reported}"
              puts "License: #{exploit.license}" if exploit.license

              if exploit.description
                puts "Description:"
                spacer

                indent do
                  exploit.description.each_line { |line| puts line }
                end
              end

              unless exploit.authors.empty?
                print_section "Authors" do
                  exploit.authors.each { |author| puts author }
                end
              end

              unless exploit.targets.empty?
                exploit.targets.each do |target|
                  print_section "Target" do
                    puts "Arch: #{target.arch}"         if target.arch
                    puts "OS: #{target.os}"             if target.os
                    puts "Software: #{target.software}" if target.software

                    if exploit.description
                      puts "Description:"

                      indent do
                        exploit.description.each_line { |line| puts line }
                      end

                      spacer
                    end
                  end

                  unless target.data.empty?
                    print_section "Target Data" do
                      target.data.each do |name,value|
                        puts "#{name} [#{value.inspect}]"
                      end
                    end
                  end
                end
              end

              begin
                exploit.load_script!
              rescue Exception => error
                print_exception error
              end

              unless exploit.params.empty?
                print_array exploit.params.values, title: 'Parameters'
              end
            end
          end

        end
      end
    end
  end
end
